//
//  YYEAnswerViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/3.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYEStudentViewController.h"
#import "YYEStudentTableViewCell.h"
#import "YYELessonAnswerModel.h"
#import "YYEStudentDetailViewController.h"

@interface YYEStudentViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) YYEBaseTableView *tableView;
@property (nonatomic,strong) NSMutableArray *datas;
@property (nonatomic,assign) NSInteger page;

@end

@implementation YYEStudentViewController

static NSString *answerCell = @"answerCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.view.backgroundColor = [UIColor greenColor];
    
    [self configUI];
    
    [self loadNewData];

    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headerView.backgroundColor = GeneralBackColor;
    self.tableView.tableHeaderView = headerView;
}

- (void)loadNewData {
    
    [self loadDataWithPage:self.page];
    
}


- (void)loadDataWithPage:(NSInteger)page {
    
    NSDictionary *dic = @{
                          @"cid":self.cid,
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
//                          @"p":@(self.page),
//                          @"size":@10
                          };
    [SVProgressHUD show];
    [AFNHelper post:GetStudent parameters:dic success:^(id responseObject) {
        

        if ([responseObject[@"status"] isEqual:@1]) {
            
            NSArray *commandArr = [YYELessonAnswerModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [self.datas addObjectsFromArray:commandArr];
            
            [self.tableView reloadData];
        }
        
        
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD showWithStatus:ShowError];
        [SVProgressHUD dismissWithDelay:0.5];
        
    }];
    
    
}

- (void)configUI {
    
    [self.view addSubview:self.tableView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[YYEStudentTableViewCell class] forCellReuseIdentifier:answerCell];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsZero);
    }];
    
}

- (YYEBaseTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[YYEBaseTableView alloc]initwithDelegate:self];
    }
    return _tableView;
    
}

- (NSMutableArray *)datas {
    
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
    
}

#pragma mark --  UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.datas.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYEStudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:answerCell];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.answerModel = self.datas[indexPath.row];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYELessonAnswerModel *answerModel = self.datas[indexPath.row];
    
    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          @"sid":answerModel.id,
                          @"cid":self.cid
                          };
    [SVProgressHUD show];
    [AFNHelper post:GetResult parameters:dic success:^(id responseObject) {
        
        //        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] isEqual:@1]) {
            
            YYELessonAnswerModel *model = [YYELessonAnswerModel mj_objectWithKeyValues:responseObject[@"data"]];
            YYEStudentDetailViewController *vc = [[YYEStudentDetailViewController alloc]init];
            vc.isBuy = @"1";
            vc.cid = self.cid;
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];

            
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD dismiss];
        
    }];
    

    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 71;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

